<?php

/**
 * SigmaCMS - Content Management System
 *
 * Copyright (C) 2008 Pavol Biely
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    SigmaCMS
 * @author     Pavol Biely <pavolbiely@gmail.com>
 * @copyright  2008 Pavol Biely
 * @license    http://www.gnu.org/licenses/gpl.txt   GNU General Public License
 * @link       http://pabi3.com/
 * @since      0.1.0
 */

final class Action
{
    /**
     * Check if is called a special method of some plugin
     * 
     * @return void
     */
	static public function initialize()
	{
		// required parameters must be set
		if (isset($_GET['plugin']) && isset($_GET['method'])) {
			// plugin & action identifiers
			$plugin = _get('plugin');
			$method = _get('method');

			// is this action valid?
			if (!ctype_alnum($plugins) && !ctype_alnum($method)) {
				throw new Sigma_Exception(_LOG_ACTION_BAD);
			}

			// get plugin instance
			if ($plugin = Plugin::getPlugin($plugin)) {
				// create method name
				$action = 'action_'.$method;

				// action exists?
				if (method_exists($plugin, $action)) {
					// event before action
					Main::addEvent('preAction', $method);

					// call action
					$plugin->$action();

					// event after action
					Main::addEvent('posAction', $method);
				} else {
					Actionlog::add(sprintf(_LOG_ACTION_UNDEFINED, _get('plugin'), _get('method')));
				}
			} else {
				Actionlog::add(sprintf(_LOG_ACTION_UNKNOWN, _get('plugin'), _get('method')));
			}
		}
	}
}
